import { ApolloServer } from 'apollo-server';

import { MySQLUtils } from './utils/requestMySQL';

import { customers, books, List } from './mock';

import { typeDefs } from './schema';

const mockCustomers = [...customers];

// Resolvers define the technique for fetching the types defined in the
// schema. This resolver retrieves books from the "books" array above.
const resolvers = {
  Query: {
    books: () => books,
    customers: async (parent: any, args: any) => {
      const result = List.getInstance().getCustomers(args);

      return result;
    },
    customer: async (parent: any, args: any) => {
      const result = List.getInstance().getDetailCustomer(args.id);

      return result;
    },
  },
  Mutation: {
    removeCustomer: async (parent: any, args: any) => {
      const result = List.getInstance().removeCustomer(args.id);

      return result;
    },
    addCustomer: async (parent: any, args: any) => {
      const result = List.getInstance().addCustomer(args);

      return result;
    },
    updateCustomer: async (parent: any, args: any) => {
      const result = List.getInstance().updateCustomer(args);

      return result;
    },
  },
};

// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({ typeDefs, resolvers });

// The `listen` method launches a web server.
interface Ires {
  url: string;
}

// const config = {
//   // host: 'mysql.seeyona9.com',
//   host: 'localhost',
//   port: 3006,
//   database: 'test',
//   user: 'root',
//   password: 'huhao123*',
// };

// MySQLUtils.getInstance().connect(config);

server.listen().then((res: Ires) => {
  console.log(`🚀  Server ready at ${res.url}`);
});
